home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / PAIR.H < prev    next >
C/C++ Source or Header  |  1992-09-23  |  7KB  |  182 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. //
  13. // Created: JCB 06/22/89 -- Design and implementation
  14. // Updated: LGO 08/15/89 -- Removed operator>>
  15. // Updated: MBN 08/20/89 -- Changed usage of template to reflect new syntax
  16. // Updated: LGO 10/15/89 -- Added first & second
  17. // Updated: LGO 10/15/89 -- Made get_first and get_second const 
  18. // Updated: MBN 12/15/89 -- Added optional argument to set_compare method
  19. // Updated: MJF 07/31/90 -- Added terse print
  20. // Updated: DLS 03/27/91 -- New lite version
  21. // Updated: JAM 08/14/92 -- removed DOS specifics, stdized #includes
  22. // Updated: JAM 08/14/92 -- modernized template syntax, remove macro hacks
  23. //
  24. // The parameterized Pair<KeyT, ValueT> class implements an association between
  25. // one object  and another. The objects  may  be of  different  types, with the
  26. // first representing the  "key" of the  pair  and the second  representing the
  27. // "value" of the pair. The Pair<KeyT, ValueT> class is used by the Association
  28. // class to  implement  an a-list,  that  is a  vector   of pairs of associated
  29. // values.
  30. //
  31. // The Pair<KeyT,  ValueT>  class is relatively  simple,  having only two  data
  32. // slots. The first slot retains the key of the pair,  and the second holds the
  33. // value.  There are three constructors for the class: a constructor that takes
  34. // no arguments and creates an empty pair object; a  constructor that takes two
  35. // initial values,  one  each for the  key  and  the value of  the  pair; and a
  36. // constructor that takes a refernece to another Pair<KeyT, ValueT>  object and
  37. // copies the values.
  38. //
  39. // Methods are provided to get and set the value of  the key and value objects,
  40. // assign one Pair<KeyT, ValueT> object to anothher,  and test for equality and
  41. // inequality between  two pairs. Finally  the output operator is overloaded to
  42. // provide a means to display the value of a pair object.
  43. //
  44.  
  45. #ifndef PAIRH
  46. #define PAIRH
  47.  
  48. #include <iostream.h>
  49.  
  50. #ifndef MISCELANEOUSH        // If we have not included this file,
  51. #include <misc.h>        // include miscelaneous useful definitions.
  52. #endif
  53.  
  54.  
  55. template <class T1, class T2>
  56. class CoolPair {
  57. public:
  58.   CoolPair();                // CoolPair p;
  59.   CoolPair(const T1&, const T2&);    // CoolPair p = (foo,bar);
  60.   CoolPair(const CoolPair<T1,T2>&);    // CoolPair p1 = p2;
  61.   ~CoolPair();                // Destructor
  62.   
  63.   inline const T1& get_first () const;        // Get first element of pair
  64.   inline const T2& get_second () const;        // Get second element of pair
  65.   inline void set_first (const T1&);        // Set first element of pair
  66.   inline void set_second (const T2&);        // Set second element of pair
  67.   inline T1& first ();                // Get reference to 1st element
  68.   inline T2& second ();                // Get reference to 2nd element
  69.   
  70.   CoolPair<T1,T2>& operator= (const CoolPair<T1,T2>&); // Assignment p1 = p2;
  71.   
  72.   inline Boolean operator== (const CoolPair<T1,T2>&) const; // is equal
  73.   inline Boolean operator!= (const CoolPair<T1,T2>& p) const; // is not equal
  74.   
  75.   void set_compare(Boolean (*) (const CoolPair<T1,T2>&, const CoolPair<T1,T2>&) = NULL); // Set compare function
  76.   
  77.   friend ostream& operator<< (ostream&, const CoolPair<T1,T2>&); // Output operator
  78.   /*##inline*/ friend ostream& operator<< (ostream&, const CoolPair<T1,T2>*);
  79.   
  80.   void print(ostream&);                // terse print
  81.  
  82. private:
  83.   T1 firstd;                    // First data slot
  84.   T2 secondd;                    // Second data slot
  85.   static Boolean (*compare_s) (const CoolPair<T1,T2>&, const CoolPair<T1,T2>&);    // Pointer operator== function
  86.   friend Boolean is_data_equal (const CoolPair<T1,T2>&, const CoolPair<T1,T2>&); 
  87. };
  88.  
  89.  
  90. // get_first -- Return the first element of the pair
  91. // Input:       None
  92. // Output:      const Reference to the first element of the pair
  93.  
  94. template<class T1, class T2> 
  95. inline const T1& CoolPair<T1,T2>::get_first () const {
  96.   return this->firstd;
  97. }
  98.  
  99.  
  100. // get_second -- Return the second element of the pair
  101. // Input:        None.
  102. // Output:       const Reference to the second element of the pair
  103.  
  104. template<class T1, class T2> 
  105. inline const T2& CoolPair<T1,T2>::get_second () const {
  106.   return this->secondd;
  107. }
  108.  
  109.  
  110. // first -- Return the first element of the pair
  111. // Input:       None
  112. // Output:      Reference to the first element of the pair
  113.  
  114. template<class T1, class T2> 
  115. inline T1& CoolPair<T1,T2>::first () {
  116.   return this->firstd;
  117. }
  118.  
  119.  
  120. // second -- Return the second element of the pair
  121. // Input:        None.
  122. // Output:       Reference to the second element of the pair
  123.  
  124. template<class T1, class T2> 
  125. inline T2& CoolPair<T1,T2>::second () {
  126.   return this->secondd;
  127. }
  128.  
  129.  
  130. // set_first -- Set the first element of the pair
  131. // Input:       Reference to a first element value
  132. // Output:      None.
  133.  
  134. template<class T1, class T2> 
  135. inline void CoolPair<T1,T2>::set_first (const T1& first) {
  136.   this->firstd = first;
  137. }
  138.  
  139.  
  140. // set_second -- Set the first element of the pair
  141. // Input:        Reference to a second element value
  142. // Output:       None.
  143.  
  144. template<class T1, class T2> 
  145. inline void CoolPair<T1,T2>::set_second (const T2& second) {
  146.   this->secondd = second;
  147. }
  148.  
  149.  
  150. // operator== -- Return TRUE if this pair and another specified are equal
  151. // Input:        Reference to a pair
  152. // Output:       TRUE or FALSE
  153.  
  154. template<class T1, class T2> 
  155. inline Boolean CoolPair<T1,T2>::operator== (const CoolPair<T1,T2>& p) const {
  156.   return (*this->compare_s)(*this, p);
  157. }
  158.  
  159.  
  160. // operator!= -- Return TRUE if this pair and another specified are not equal
  161. // Input:        Reference to a pair
  162. // Output:       TRUE or FALSE
  163.  
  164. template<class T1, class T2> 
  165. inline Boolean CoolPair<T1,T2>::operator!= (const CoolPair<T1,T2>& p) const {
  166.   return !(*this->compare_s)(*this, p);
  167. }
  168.  
  169.  
  170. // operator<< -- Overload output operator for the pair class
  171. // Input:        Pointer to a pair object, reference to an output stream
  172. // Output:       Reference to an output stream
  173.  
  174. template<class T1, class T2>
  175. inline ostream& operator<< (ostream& os, const CoolPair<T1,T2>* p) {
  176.   return operator<< (os, *p);
  177. }
  178.  
  179. #include <cool/Pair.C>   // required for most template implementations
  180.  
  181. #endif                        // End #ifndef PAIRH
  182.